module("Loading", package.seeall);
require "Project/Engine/LoadItems"

-- 加载文件类型
RAW = 0; -- 原始数据，文本和二进制，参数add_search_path表示是否要将路径加入搜索路径，返回string
FONT = 1; -- 字体数据，返回字体图片文件路径
UI = 2; -- UI数据，返回UILayout
SCENE = 3; -- 场景文件，返回CCNode
IMAGE = 4; -- 图片文件，返回CCTexture2D
ARMATURE_CLASS = 5; -- 动画文件定义，用于预加载动画, 参数is_effect表示传入的名称是否为特效
ARMATURE = 6; -- 动画，加载最好先加载对应的ARMATURE_CLASS, 参数is_effect表示传入的名称是否为特效
PLIST = 7; -- PLIST定义文件（含对应的贴图）
XML = 8; -- XML文件，返回对应的XML结构，注意CPP部分使用的XML应使用RAW加载
PB = 9; -- PB定义文件
PBCONST = 10;-- PB常量定义文件
MUSIC = 11;-- 音乐，返回音乐文件名
POOLED_ARMATURE = 12;-- 动画(放入animationPool中)，加载最好先加载对应的ARMATURE_CLASS, 参数is_effect表示传入的名称是否为特效
TILEDMAP = 13;-- Tiled Map
NEWXML = 14;-- 新的xml规则
NEWUI = 15;
-- 顺序必须与上面的定义相同
local _typeTexts = {
"raw",
"font",
"ui",
"scene",
"image",
"armature_class",
"armature",
"plist",
"xml",
"pb",
"pbconst",
"music",
"pooled_armature",
"tiled_map",
"new_xml",
"new_ui"
};
-- 加载
-- SUCCESS				= 0;
-- FAILED 				= 1;

local _moduleListener = nil;

local _loadedFuncs = nil;
local _ratio = 1;
local _progress = 0;
local _simpleScene = nil;
local _needUI = true;
-- local _stopIfFailed	= true;
local _totalWeight = 0;
local _weight = 0;

local _files = { };
local _loadItems = nil;
local _index = 1;
local _timerId = 0;

_scene = nil;
local _director = nil;
local _scheduler = nil;
local _uiLayer = nil;
local _loadingUI = nil;

local _hintTexts = { };
local _needReleaseTypes = { UI, SCENE, IMAGE, ARMATURE, POOLED_ARMATURE, TILEDMAP, NEWUI };

local LOADING = 0;
local LOADED = 1;
local POSTLOADED = 2;
local ENDPOSTLOADED=3;
_state = nil;
local _moduleScene = nil;
local _params = nil;
local _module = nil;

local LuaHasError = false
postLoadCompleteFunc = nil;
isLoading = false;
loadCache={};
local function onLoadError(msg)
	-- LuaHasError = true
	print("----------------------------------------")
	print("LUA ERROR: " .. tostring(msg) .. "\n")
	print(debug.traceback())
	print("----------------------------------------")
end

local function stopUpdate()
	if _timerId > 0 then
		_scheduler:unscheduleScriptEntry(_timerId);
		_timerId = 0;
	end
end

local function isNeedReleaseType(t)
	for _, v in ipairs(_needReleaseTypes) do
		if v == t then return true end
	end
	return false;
end

function clearCache()
	for _,v in pairs(loadCache) do
		
	end
	loadCache={};
end

function clear()
	_files = { };
end


local function updatePercent(percent)
	if percent >= 100 then
		
	end
end

local function loadFile(file)
	
end

local function update(dt)
	if LuaHasError then return end
	if _simpleScene~=nil then
		Application.LoadLevel(_simpleScene);
		_index = 1;
		updatePercent(_index*100/(#_files+1));
	end
	for k, v in ipairs(_files) do
		loadFile(v);
		_index=_index+1;
		updatePercent(_index*100/(#_files+1));
	end
	
	_loadedFuncs(_loadItems,_params);
end


function beginQueue(loadedFuncs, params, module, simpleScene)
	
	clear();
	_loadedFuncs = loadedFuncs;
	_params = params;
	_module = module;
	_simpleScene = simpleScene;
end

-- filelist is array, its element is like { filename="xml/1.xml", type = Loading.RAW, weight=1, id="xml", add_search_path = false }
function addFiles(filelist)
	if not filelist then return end
	for _, v in ipairs(filelist) do
		addFile(v.filename, v.type, v.weight, v.id, v.params,v.cache,v.num);
	end
end
function addFile(filename, type, weight, id, params,cache,num)
	if weight == nil or weight < 1 then weight = 1 end
	if id == nil then id = filename end
	if add_search_path == nil then add_search_path = false end
	_totalWeight = _totalWeight + weight;
	_files[#_files + 1] = { id = id, filename = filename, type = type, weight = weight, params = params,cache=cache ,num=num};
end

function endQueue()
	if _loadItems == nil then _loadItems = LoadItems.new() end
	-- _timerId = _scheduler:scheduleScriptFunc(update, _director:getDeltaTime(), false)
	_index = 0;
	_state = LOADING;
	Application.LoadLevel("LoadingScene");
	local co=coroutine.create(update);
	coroutine.resume(co);
	updatePercent(0);
	_G["__G__TRACKBACK__"] = onLoadError
end


function loadModule(module, simpleScene,params)
	local loadedFuncs = module.create ;
	beginQueue(loadedFuncs, params, module, simpleScene);
	local loadFiles =(module.getLoadFiles == nil) and { } or module.getLoadFiles(params);
	addFiles(loadFiles);
	endQueue();
	isLoading = true;
end
